﻿

<h1>Ajax</h1>
<script src="/lib/jquery/dist/jquery.js"></script>
<script type="text/javascript">
    function hashcode(str) {
        var hash = 0, i, chr, len;
        if (str.length === 0) return hash;
        for (i = 0, len = str.length; i < len; i++) {
            chr = str.charCodeAt(i);
            hash = ((hash << 5) - hash) + chr;
            hash |= 0; // Convert to 32bit integer
        }
        return hash;
    }

    $(function () {
        var _Ajax = $.ajax,
            _Queue = [],
            _timers = [];
        $.extend({
            guid: function () {
                var d = new Date().getTime();
                var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
                    var r = (d + Math.random() * 16) % 16 | 0;
                    d = Math.floor(d / 16);
                    return (c == 'x' ? r : (r & 0x3 | 0x8)).toString(16);
                });
                return uuid;
            },
            ajax: function (option, type) {
                return new newAjax(option).init();
            }
        });

        function newAjax(option)
        {
            this.Url = option.url;
            this.Option = option;
            this.RequestId = $.guid();
            if (!_Queue[this.Url]) {
                _Queue[this.Url] = [];
            }
            this.Queue = _Queue[this.Url];
        }
        newAjax.prototype = {
            constructor: newAjax,
            init: function ()
            {
                var _this = this;
                if (!_this.validate()) {
                    return;
                }
                var request = _this.addNewRequest();
                _this.abort();
                _this.overrideEvent();
                request.Value = _Ajax(_this.Option);
                return request.Value;
            },
            validate: function ()
            {
                var _this = this;
                if (_this.Url.length == 0) {
                    throw Exception('无效的请求')
                    console.log('无效的请求');
                    return false;
                }
                return true;
            },
            abort: function ()
            {
                var len = this.Queue.length - 1;
                for (var i = len; i > 0; i--) {
                    if (this.Queue[i].Value) {
                        this.Queue[i].Value.abort();
                        this.Queue.splice(i, 1);
                    }
                }
            },
            overrideEvent: function ()
            {
                var _this = this;
                var _success = _this.Option.success,
                    _error = _this.Option.error,
                    _complete = _this.Option.complete;
                //重写事件
                _this.Option.success = function (data, textStatus, jqXHR) {
                    if (_Queue[_this.Url] && _Queue[_this.Url].length > 0) {
                        var lastGuid = _Queue[_this.Url][0].Key;
                        if (_this.RequestId == lastGuid) {
                            _Queue[_this.Url] = [];
                            _success(data, textStatus, jqXHR);
                        }
                    }
                };
                _this.Option.error = function (data, textStatus, jqXHR) {
                    if (textStatus == "abort") {
                        return;
                    }
                };
                _this.Option.complete = function (data, textStatus, jqXHR) {
                    if (textStatus == "abort") {
                        return;
                    }
                    _complete(data, textStatus, jqXHR);
                };
            }
        };
        newAjax.prototype.addNewRequest = function () {
            var newRequest = { Key: this.RequestId, Value: null };
            this.Queue.unshift(newRequest);
            return newRequest;
        }
        var j = 0;
        for (var i = 0; i < 5; i++) {
            

            setTimeout((i) => {
                j++;
                var _re = $.ajax({
                    method: "post",
                    url: "/home/ajax",
                    data: {j:j},
                    success: function (data, textStatus, jqXHR) {
                        console.log(jqXHR);
                        console.log(`success:`,i);
                    },
                    error: function (jqXHR, textStatus, errorThrown) {
                        console.log(arguments);

                        console.log(`error`);
                    },
                    complete: function (jqXHR, textStatus) {
                        console.log(`complete`);
                    }
                });
            }, parseInt(Math.random() * 2000, 10),i);
        }
    });



</script>
